package com.bw.goods.service.impl;

import com.bw.common.utils.R;
import com.bw.goods.config.IdWorker;
import com.bw.goods.config.MyRedissonConfig;
import com.bw.goods.dao.GoodsDao;
import com.bw.goods.entity.GoodsEntity;
import com.bw.goods.entity.vo.MiaoShaVO;
import com.bw.goods.feignService.feignService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.common.utils.PageUtils;
import com.bw.common.utils.Query;

import com.bw.goods.dao.OrderInfoDao;
import com.bw.goods.entity.OrderInfoEntity;
import com.bw.goods.service.OrderInfoService;
import org.springframework.transaction.annotation.Transactional;


@Service("orderInfoService")
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoDao, OrderInfoEntity> implements OrderInfoService {

    @Autowired
    private OrderInfoDao orderInfoDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private feignService feignService;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderInfoEntity> page = this.page(
                new Query<OrderInfoEntity>().getPage(params),
                new QueryWrapper<OrderInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public R add(MiaoShaVO miaoShaVO) {
        RLock orderLock = redissonClient.getLock("orderLock");
        try {
            //加锁
            orderLock.lock();
//            boolean b = orderLock.tryLock(30, 10, TimeUnit.MICROSECONDS);

            String stockCount = redisTemplate.boundValueOps("stockCount").get();
            int m = Integer.parseInt(stockCount);
            if(m == 0) {
                return R.error("库存不足");
            }
            OrderInfoEntity orderInfoEntity = new OrderInfoEntity();
            //商品订单
            //雪花算法生成商品订单id
            long nextId = idWorker.nextId();
            String orderId = String.valueOf(nextId);
            orderInfoEntity.setOrderId(orderId);
            //商品id
            orderInfoEntity.setGoodsId(miaoShaVO.getId());
            //秒杀商品名称
            orderInfoEntity.setGoodsName(miaoShaVO.getGoodsName());
            //秒杀价格
            orderInfoEntity.setGoodsPrice(miaoShaVO.getMiaoshaPrice());
            //秒杀数量
            orderInfoEntity.setGoodsCount(1);
            //收货地址
            orderInfoEntity.setDeliveryAddrId(1);
            //订单来源平台
            orderInfoEntity.setGoodsChannel(1);
            //订单创建时间
            orderInfoEntity.setCreateDate(new Date());
            //用户id
            orderInfoEntity.setUserId(miaoShaVO.getUserId());
            //订单状态
            orderInfoEntity.setStatus(0);

            //创建订单
            orderInfoDao.insert(orderInfoEntity);
            //根据商品id查询出商品对象
            GoodsEntity goodsEntity = goodsDao.selectById(miaoShaVO.getId());
            //库存减一
            goodsEntity.setGoodsStock(goodsEntity.getGoodsStock()-1);
            //修改商品对象
            goodsDao.updateById(goodsEntity);
            System.out.println("购买成功");
            //redis数量减一
            redisTemplate.boundValueOps("stockCount").set(String.valueOf(m-1));
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            orderLock.unlock();
        }

        return R.error();
    }
}